package com.datang.litejvm.print;

import com.datang.litejvm.clz.ClassAccessFlag;
import com.datang.litejvm.clz.Class;

/**
 * @author: 顶风少年
 * @Description: 仿照 javap
 * @date: 20:38 2022/6/13
 **/
public class ClassFilePrinter {
    Class clazz;

    public ClassFilePrinter(Class clzFile) {
        this.clazz = clzFile;
    }

    public void print() {

        System.out.println("  minor version:" + clazz.getMinorVersion());
        System.out.println("  major version:" + clazz.getMajorVersion());
        ClassAccessFlag classAccessFlag = clazz.getAccessFlag();
        StringBuilder sb = new StringBuilder();
        sb.append("  flags: ");
        if (clazz.getAccessFlag().isPublic()) {
            sb.append("ACC_PUBLIC, ");
        }
        if (clazz.getAccessFlag().isFinal()) {
            sb.append("ACC_FINAL, ");
        }
        if (clazz.getAccessFlag().isSuper()) {
            sb.append("ACC_SUPER, ");
        }
        if (clazz.getAccessFlag().isInterface()) {
            sb.append("ACC_INTERFACE, ");
        }
        if (clazz.getAccessFlag().isAbstract()) {
            sb.append("ACC_ABSTRACT, ");
        }
        if (clazz.getAccessFlag().isSynthetic()) {
            sb.append("ACC_SYNTHETIC, ");
        }
        if (clazz.getAccessFlag().isAnnotation()) {
            sb.append("ACC_ANNOTATION, ");
        }
        if (clazz.getAccessFlag().isEnum()) {
            sb.append("ACC_ENUM, ");
        }
        sb.delete(sb.length() - 2, sb.length());
        System.out.println(sb.toString());

        ConstantPoolPrinter cnstPoolPrinter = new ConstantPoolPrinter(clazz.getConstantPool());
        cnstPoolPrinter.print();
    }
}
